package android.app.gfestival.component;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.ProgressDialog;
import android.app.gfestival.R;
import android.app.gfestival.R.id;
import android.app.gfestival.R.layout;
import android.app.gfestival.R.style;
import android.app.gfestival.adapter.DbHelper;
import android.app.gfestival.adapter.MyAlphabetizedAdapter;
import android.app.gfestival.adapter.ProgramFistAdapter;
import android.app.gfestival.adapter.ProgramSecondAdapter;
import android.app.gfestival.rss.RSSFeed;
import android.app.gfestival.soap.Artist;
import android.app.gfestival.soap.ArtistDescription;
import android.app.gfestival.soap.SoapParserHelper;
import android.content.Context;
import android.database.Cursor;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class ArtistViewSwicher extends ViewSwitcher {

	private static final String TAG = "ArtistViewSwicher";
	private static final Integer SHOW_NEXT = 2;
	private static final Integer INIT = 1;
	private LayoutInflater inflater;
	private LinearLayout secondScreen;
	private String lastData = "";
	private boolean isSecondScreen;
	private int currentPosition;
	private MyAlphabetizedAdapter fistScreenAdapter;
	private int position;
	private Activity context;
	private ProgressDialog progress;

	public ArtistViewSwicher(Activity context, Cursor cursor) {
		super(context);
		this.context = context;
		currentPosition = 0;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		LinearLayout fistScreen = (LinearLayout) inflater.inflate(
				R.layout.artist_fist_screen, null);
		ListView fistScreenListView = (ListView) fistScreen
				.findViewById(R.id.listview_artist);
		fistScreenAdapter = new MyAlphabetizedAdapter(context,
				R.layout.aritist_adapter, cursor, new String[] {
						DbHelper.ARTIST_NAME, DbHelper.ARTIST_SHORT_DES },
				new int[] { R.id.textview_name, R.id.textview_time });
		fistScreenListView.setAdapter(fistScreenAdapter);
		fistScreenListView
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int position, long arg3) {
						showNext(position);
					}
				});
		this.addView(fistScreen);
		this.setOutAnimation(context, android.R.anim.slide_out_right);
		this.setInAnimation(context, android.R.anim.slide_in_left);
	}

	public int showPreviou() {
		currentPosition--;
		if (currentPosition < 0)
			return currentPosition;
		showPrevious();
		return currentPosition;
	}

	public void showNext(int position) {
		progress = showProgressDialog();
		Cursor c = ((Cursor) fistScreenAdapter.getItem(position));
		String id = c.getString(c.getColumnIndex(DbHelper.ARTIST_ID));
		this.position = position;
		currentPosition++;
		if (lastData.equals(id)) {
			showNext();
			progress.dismiss();
		} else {
			new ArtistAsynTask().execute(id);
		}
	}

	private String stringToImgTag(String data) {
		data = "<img src=\"" + data + "\"/>";
		return data;
	}

	private String stringToHTML(String data) {
		String openDivTag = "<?xml version=\"1.0\" encoding=\"iso-8859-1\" ?><html><head> <meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" /></head><body><div style=\"color: white;font-size: 14pt;\" align=\"center\">";
		String closeDivTag = "</div></body></html>";
		data = data.replace("\n", "<br/>");
		data = openDivTag + data + closeDivTag;
		Log.i("", "------>" + data);
		return data;
	}

	private WebView getWebView(View parent, int resid) {
		WebView mWebView = (WebView) parent.findViewById(resid);
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setSavePassword(false);
		webSettings.setSaveFormData(false);
		webSettings.setJavaScriptEnabled(false);
		webSettings.setSupportZoom(false);
		mWebView.setBackgroundColor(0);
		return mWebView;
	}

	private class ArtistAsynTask extends
			AsyncTask<String, String, List<Artist>> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected List<Artist> doInBackground(String... params) {
			String id = params[0];
			List<Artist> ids = new ArrayList<Artist>();
			try {
				ids = SoapParserHelper.getArtistDetailById(id);
			} catch (Exception e) {
				e.printStackTrace();
			}
			lastData = id;
			return ids;
		}

		@Override
		protected void onPostExecute(List<Artist> ids) {
			if (secondScreen != null)
				removeView(secondScreen);
			secondScreen = (LinearLayout) inflater.inflate(
					R.layout.artist_detail, null);
			if (ids.size() > 0) {
				TextView tvArtistName = (TextView) secondScreen
						.findViewById(R.id.artist_name);
				TextView tvArtistShortDest = (TextView) secondScreen
						.findViewById(R.id.artist_short_desc);
				Artist a = ids.get(0);
				tvArtistName.setText(a.artist_name);
				tvArtistShortDest.setText(a.artist_short_description);
				WebView mWebViewImg = getWebView(secondScreen,
						R.id.webview_artist_img);
				// mWebViewImg.loadUrl(a.artist_picture);
				mWebViewImg.loadData(
						stringToHTML(stringToImgTag(a.artist_picture)), "text/html",
						"iso-8859-1");
				WebView mWebViewDesc = getWebView(secondScreen,
						R.id.webview_artist_desc);
				mWebViewDesc.loadData(stringToHTML(a.artist_full_description),
						"text/html", "iso-8859-1");
				addView(secondScreen);
			}
			showNext();
			progress.dismiss();
		}

	}

	public ProgressDialog showProgressDialog() {
		ProgressDialog progress = new ProgressDialog(this.context.getParent(),
				R.style.ProgressDialogSpinner);
		progress.setMessage("Loading...");
		progress.show();
		return progress;
	}

	@Override
	public void showNext() {
		super.showNext();
	}

}
